import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyStoryServer {

	ServerSocket server;
	Socket client;
	BufferedReader in;
	PrintWriter out;
	String line;

	public static void main(String... args) {
		new MyStoryServer().run();
	}

	public void run() {

		try {
			server = new ServerSocket(1228);
		} catch (IOException e) {
			System.out.println("Could not listen on port 1228");
			System.exit(-1);
		}

		System.out.println("The Server is listening to port 1228");

		// listenSocketSocketserver.acceptSocket
		try {
			client = server.accept();
		} catch (IOException e) {
			System.out.println("Accept failed: " + client.getPort());
			System.exit(-1);
		}

		// listenSocketBufferedReaderclientPrintWriter
		try {
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			out = new PrintWriter(client.getOutputStream(), true);
		} catch (IOException e) {
			System.out.println("Read failed");
			System.exit(-1);
		}

		System.out.println("The Server is listening to port 1228");
		// listenSocket
		while (true) {
			try {
				line = in.readLine();
				System.out.println(line);
				// Send data back to client
				out.println("Yes, this is.");

			} catch (IOException e) {
				System.out.println("Read failed");
				System.exit(-1);
			}
		}
	}

}
